package model.service;

import model.dao.BaseDao;
import model.dao.SearchDao;
import model.dao.SearchDaoImpl;
import model.entity.Book;
import org.junit.Test;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;

public class SearchServiceImpl implements SearchService{

    private JdbcTemplate template;
    private SearchDao searchDao;
    private AddService addService;
    public SearchServiceImpl() {
        template=new JdbcTemplate(BaseDao.getDataSource());
        searchDao=new SearchDaoImpl(template);
        addService=new AddServiceImpl();
    }

    @Override
    public List<Book> search(String bookNo, String bookName) {
        List<Book> books=new ArrayList<>();;
        Book book;
       if(bookName.length()!=0)
       {
           books = searchDao.searchByName(bookName);
       }

       if(bookNo.length()!=0)
       {
           if (addService.isExist(bookNo)) {
               book = searchDao.searchByNo(bookNo);
               books.add(book);
           }
       }

       return books;
    }

    @Test
    public void test()
    {
         List<Book> books=search("1","0000001");
        System.out.println(books);
    }
}
